#    This is part of WeatherFront.
#
#    Copyright (C) 2010 Stefan Sieber
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; version 2 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

import objc
import string
from PyFR.BackRow import *
import Foundation



verbose=1
def log(s,level=1):
	if verbose >= level:
		Foundation.NSLog( u"%s: MetaData: %@", "WeatherFrontLog", s ) 
	pass

class WeatherFrontMediaAsset(BRSimpleMediaAsset):

	def initWithItem_(self, weatherItem):
		log("WeatherFrontMediaAsset::initWithItem")
		BRSimpleMediaAsset.initWithMediaURL_(self,"")
		self.item=weatherItem
		return self
		

	def coverArt(self):
		log("WeatherFrontMediaAsset::coverArt")
		return BRImage.imageWithPath_(self.item.GetPreviewImagePath())


class WeatherFrontMetadataPopulator(NSObject):

	
	def populateLayer_fromAsset_(self, layer, asset):
		log("WeatherFrontMetadataPopulator::populateLayer_fromAsset")
		
		
		if asset.item.id == "cc":
			layer.setTitle_( "Current Conditions" )
			layer.setSummary_( ("%s° %s - %s" % ( asset.item.temp1, asset.item.unitTemp, asset.item.text) ) )
			labels=[
				"Feels like",
				"Wind",
				"Location",
				"Last Update"
				]
			data=[
				("%s° %s" % (asset.item.temp2, asset.item.unitTemp)),
				("from %s" % (asset.item.windFrom)),
				asset.item.location,
				asset.item.date
				]
		else:
			layer.setTitle_( ("Forecast for %s, %s" % ( asset.item.caption, asset.item.date) ) )
			layer.setSummary_( ("%s" % asset.item.text ) )
			labels=[
				"High",
				"Low",
				]
			data=[
				("%s° %s" % (asset.item.temp1, asset.item.unitTemp)),
				("%s° %s" % (asset.item.temp2, asset.item.unitTemp)),
				]
				

		layer.setMetadata_withLabels_(data,labels)
		return

	def axMetadataFromAsset_(self, asset):
		log("called axMetadataFromAsset %s" % repr(asset))
		return None

class WeatherFrontMetadataPopulatorFactory(BRSingleton):
	"""
	Custom metadata populator factory which will return a populator which understands EyeTV recordings
	"""

	__Instance = None

	def init(self):
		log("WeatherFrontMetadataPopoulatorFactory::init")
		self=super(WeatherFrontMetadataPopulatorFactory,self).init()
		if self is None:
			return None
		__Instance=self
		self.pop=WeatherFrontMetadataPopulator.alloc().init()
		return self

	# Note: this isn't really a good singleton implementation, but it's good enough for our purposes here
	@classmethod
	def singleton(self):
		return self.__Instance

	def populatorForAsset_(self, asset):
		return self.pop

# create instance of this to use below
newPopFactory=WeatherFrontMetadataPopulatorFactory.alloc().init()

class WeatherFrontPreviewMetadataController(BRMetadataPreviewController):

	def initWithItem_(self, channel):
		log("metadataController::initWithChannel_: %s" % repr(self))
		self=super(WeatherFrontPreviewMetadataController,self).init()
		if self is None:
			return None
		asset=WeatherFrontMediaAsset.alloc().initWithItem_(channel)
		self.setAsset_(asset)
		self.setShowsMetadataImmediately_(True) 
		return self

	def _updateMetadataLayer(self):
		log("WeatherFrontPreviewMetadataLayerController::_updateMetadataLayer")

		# The standard populator doesn't understand our assets, so we
		# temporarily replace the std populator factory with our populator
		# factory.	(and yes, this is a horrible abuse of setSingleton_.)

		oldPopFactory=BRMetadataPopulatorFactory.sharedInstance()
		BRMetadataPopulatorFactory.setSingleton_(newPopFactory)
		BRMetadataPreviewController._updateMetadataLayer(self)
		BRMetadataPopulatorFactory.setSingleton_(oldPopFactory)

	def dealloc(self):
		log("metadataController dealloc for %s" % repr(self))
		if self.asset() is not None:
			log("releasing asset %s" % repr(self.asset()))
			#self.asset().release()
		super(WeatherFrontPreviewMetadataController,self).dealloc()


